<?php
namespace PsiFramework\Applications\DefaultApp\Classes\Actions\Message;

use PsiFramework\Applications\DefaultApp\Classes\Model\Message;

use PsiFramework\Classes\Factory\DAOFactory;

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Util\Parameters\Action\ActionParameters;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de message
 *
 * @package Actions.Message
 */
class ShowMessage extends MessageAction
{

    /**
     *
     * @return bool
     */
    public function action()
    {
        $ok = parent::action();
    
        if ($ok) {
            $messageId = $this->_request->getParameter('id');
            
            $messageDao = DAOFactory::getDAO('Message');
            $message = $messageDao->retrieveByPk($messageId);
            
            if (!is_null($message)
                && ((!is_null($message->getSender()) && $message->getSender()->getId() == $this->_user->getId())
                    || (!is_null($message->getRecipient()) && $message->getSender()->getRecipient() == $this->_user->getId())
                )
            ) {
                $this->_response->setAttribute('message', $message);
                $ok = true;
            } else {
                $ok = false;
            }
        }
        
        return $ok;
    }
    
    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     *
     */
    public function execute()
    {
        parent::execute();
        
        if (!$this->action()) throw new \Exception();

        $this->findForward("success");
    }
}
